local fusong = fk.CreateSkill {
  name = "fusong",
}

fusong:addEffect(fk.Death, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self, false, true) and
      table.find(player.room:getOtherPlayers(player, false), function(p)
        return p.maxHp > player.maxHp and not (p:hasSkill("fengzi", true) and p:hasSkill("jizhan", true))
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function(p)
      return p.maxHp > player.maxHp and not (p:hasSkill("fengzi", true) and p:hasSkill("jizhan", true))
    end)
    local to = room:askToChoosePlayers(player,{targets=table.map(targets, Util.IdMapper),min_num=1,max_num=1,prompt="#fusong-choose",
      skill_name=self.name,cancelable=true})
    if #to > 0 then
      event:setCostData(self,{tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local choices = {}
    for _, s in ipairs({"fengzi", "jizhan"}) do
      if not to:hasSkill(s, true) then
        table.insert(choices, s)
      end
    end 
    local choice = room:askToChoice(to,{choices=choices,skill_name=self.name,prompt="#fusong-choice"})
    room:handleAddLoseSkills(to, choice, nil, true, false)
  end,
})

return fusong